package org.example.controller;

import org.example.domain.vo.*;
import org.example.pojo.entity.Memory;
import org.example.pojo.entity.User;
import org.example.result.Result;
import org.example.service.MemoryService;
import org.example.service.InterviewAccordingToCompanyService;
import org.example.service.PdfLoaderService;
import org.example.service.RedisVectorService;
import org.example.service.impl.TopicAboutCompanyServiceImpl;
import org.example.util.CreateModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import dev.langchain4j.model.openai.OpenAiModerationModel;

@RequestMapping("/llm")
@RestController
public class llmController {

    @Autowired
    InterviewAccordingToCompanyService interviewAccordingToCompanyService;
    @Autowired
    MemoryService memoryService;
    @Autowired
    RedisVectorService redisVectorService;
    @Autowired
    PdfLoaderService pdfLoaderService;
    @Autowired
    CreateModel createModel;
    @Autowired
    TopicAboutCompanyServiceImpl topicAboutCompanyService;

    @GetMapping("importTopicsFromFile")
    public Result<String> importTopicsFromFile(){
        topicAboutCompanyService.importTopicsFromFile();
        return Result.success("success!");
    }

    @GetMapping("/getTopicAboutCompany")
    public Result<List<String>> getTopicAboutCompany(String companyName) {
        return interviewAccordingToCompanyService.getTopicAboutCompany(companyName);
    }

    @PostMapping("/summarizeKeyWordsAccordingToTheTopic")
    public Result<List<String>> summarizeKeyWordsAccordingToTheTopic(@RequestBody String[] topic){
        return interviewAccordingToCompanyService.summarizeKeyWordsAccordingToTheTopic(Arrays.asList(topic));
    }

    @PostMapping("/topicalAccordingToTheKeyWord")
    public Result<List<String>> topicalAccordingToTheKeyWord(@RequestBody String[] keyWords){
        return interviewAccordingToCompanyService.topicalAccordingToTheKeyWord(Arrays.asList(keyWords));
    }

    @PostMapping("/addMemory")
    public Result<String> addMemory(@RequestBody Memory memory){
        memoryService.addMemory(memory);
        return Result.success("success");
    }

    @PostMapping("/interviewer")
    public Result<String> interviewer(@RequestBody InterviewRequest interviewRequest){
        return interviewAccordingToCompanyService.interview(interviewRequest);
    }

    @PostMapping("/searchAnswer")
    public Result<Map<String, List<String>>> searchAnswer(@RequestBody String[] questionList){
        return interviewAccordingToCompanyService.searchAnswer(Arrays.asList(questionList));
    }

    @PostMapping("/storeAndEmbeddingAnswerAndQuestion")
    public Result<String> storeAndEmbeddingAnswerAndQuestion(@RequestBody String[] questionList){
        return interviewAccordingToCompanyService.storeAndEmbeddingAnswerAndQuestion(Arrays.asList(questionList));
    }

    @GetMapping("/summarizeInterview")
    public Result<String> summarizeInterview(){
        return interviewAccordingToCompanyService.summarizeInterview();
    }

    @PostMapping("/generateAndStoreStandardAnswers")
    public Result<String> generateAndStoreStandardAnswers(@RequestBody SearchAnswerRequest questions) {
        return interviewAccordingToCompanyService.generateAndStoreStandardAnswers(questions);
    }

    @PostMapping("/calculateAnswerSimilarity")
        public Result<Double> calculateAnswerSimilarity(@RequestBody SimilarRequest similarRequest) {
        return interviewAccordingToCompanyService.calculateAnswerSimilarity(similarRequest.getQuestion(), similarRequest.getUserAnswer());
    }

    /**
     * 开始面试
     * @param startInterviewRequest 包含题目列表的请求
     * @return 第一个面试题目
     */
    @PostMapping("/startInterview")
    public Result<String> startInterview(@RequestBody StartInterviewRequest startInterviewRequest) {
        return interviewAccordingToCompanyService.startInterview(startInterviewRequest.getTopics());
    }

    /**
     * 提交用户回答
     * @param submitAnswerRequest 包含用户回答的请求
     * @return 反馈或下一个题目
     */
    @PostMapping("/submitAnswer")
    public Result<String> submitAnswer(@RequestBody SubmitAnswerRequest submitAnswerRequest) {
        // 检查请求和请求中的answer是否为null
        if (submitAnswerRequest == null) {
            return Result.error("请求不能为空");
        }
        
        String answer = submitAnswerRequest.getAnswer();
        if (answer == null || answer.trim().isEmpty()) {
            return Result.error("答案不能为空");
        }
        
        // 使用验证过的答案调用服务
        return interviewAccordingToCompanyService.handleUserAnswer(answer);
    }

    @GetMapping("/redisVector")
    public Result<String> redisVector(){
        return redisVectorService.processAndStoreContent();
    }

    @GetMapping("/pdfLoader")
    public Result<String> pdfLoader(){
        return pdfLoaderService.exportPdfSectionsToRedis();
    }

    @PostMapping("/summarizeResume")
    public Result<List<String>> summarizeResume(@RequestBody ReadFileRequest readFileRequest) throws IOException {
        return interviewAccordingToCompanyService.summarizeResume(readFileRequest.getFilePath());
    }

    @PostMapping("/test")
    public Result<?> test(){
        OpenAiModerationModel openAiModerationModel = createModel.createModerationModel();
        return Result.success(openAiModerationModel.moderate("I want to kill them!!"));
    }

    @PostMapping("/readWeakKnowledgePointsAndAnalyze")
    public Result<List<String>> readWeakKnowledgePointsAndAnalyze(@RequestBody User user){
        return interviewAccordingToCompanyService.readWeakKnowledgePointsAndAnalyze(user);
    }

}